package cn.edu.swu.film;

import cn.edu.swu.db.DBEngine;
import cn.edu.swu.db.RecordVisitor;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/filmsave")
public class Savefilm extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String star = request.getParameter("star");
        String grade = request.getParameter("grade");
        String time = request.getParameter("time");
        String describe = request.getParameter("describe");

        Film film = new Film();
        film.setName(name);
        film.setStar(star);
        film.setGrade(Float.valueOf(grade));
        film.setDescribe(describe);
        film.setTime(time);
        System.out.println(String.format("%s, %s, %s, %s, %s", name, star, grade, time, describe));

        String message = null;
        try {
            FilmRepo.getInstance().savefilm(film);
            message = "提交信息保存成功！";
        } catch (SQLException e) {
            e.printStackTrace();
            message = "提交信息保存失败！";
        }

        response.setContentType("text/html; charset=UTF-8");
        try (Writer writer = response.getWriter()) {
            String html = "<center style=‘margin-top:5em’><h1>%s</h1><br><br>" +
                    "<a href='./filmsave.html'>再 次 录 入</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<a href='./index.html'>返 回 首 页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<a href='./listfilm'>查看列表</a>" +
                    "</center>";
            writer.write(String.format(html, message));
        }
    }

}



